$(document).ready(function() {	

	// var jsonObject = $.parseJSON('{"name": "John"}');
	// console.log(jsonObject);

	/*---------Start of Toppanel username/logout changing code---------*/

	var locationSearch = window.location.search;
	var helloGuest = 'Guest';
	var ulLoginLi = $('div.tab ul.login li');
	if(locationSearch != '') {
		var jsonFromPathSearch = locationSearch.split('?')[1].replace(/%22/g, "\"");
		$('#toggle a').each(function() {
			var $this = $(this);
			if($this.hasClass('logout_a')) {
				$this.show();
			} else {
				$this.hide();
			}
		});
		var loginFromJson = $.parseJSON(jsonFromPathSearch).login;
		ulLoginLi.eq(1).text('Logged in as ' + loginFromJson  + '!');
	} else {
		ulLoginLi.eq(1).text('Hello ' + helloGuest + '!');
	}

	/*---------End of Toppanel username/logout changing code---------*/

	/*-----Start of login/password input changes-----*/

	var passwordText = $('#passwordText');
	var loginPass = $('#login_pass');
	var loginName = $('#login_name');
	var usernameText = 'Type your username here';

	loginName.css('color', 'gray');

	loginName.focus(function() {
		if(loginName.val() == usernameText) {
			loginName.val('');
		}
		loginName.css('color', 'white');
	});

	loginName.blur(function() {
		if(loginName.val() == '') {
			loginName.css('color', 'gray');
	        loginName.val(usernameText);
	    }
	});

	passwordText.show();
	loginPass.hide();
	 
	passwordText.focus(function() {
	    passwordText.hide();
	    loginPass.show();
	    loginPass.focus();
	});
	loginPass.blur(function() {
	    if(loginPass.val() == '') {
	        passwordText.show();
	        loginPass.hide();
	    }
	});

	/*-----End of login/password input changes-----*/

	/*-------Start of login check code-------*/
	

	$('#login_form').submit(function() {
		// username value
		var username = $('#login_name').val();
		// password value
		var password = $('#login_pass').val();
		// error field
		var errorMessage = $('#login_error');

		var newPathName = '';

		if (username.length < 1 || password.length < 1) {
		    errorMessage.text('Please, enter data...').show();
		    return false;
		} else if (username != 'dynamics' || password != 'alex') {
			errorMessage.text('The username or password is incorrect...').show();
		    return false;
		} else {
			loginAndPassword = new Object();
			loginAndPassword.login = username;
			// newPathName = window.location.pathname + "?username=" + username;
			newPathName = window.location.pathname + "?" + JSON.stringify(loginAndPassword);
			document.forms[0].action = newPathName;
			// document.forms[0].submit();
			return true;
		}

	});

	/*-------End of login check code-------*/

	/*-------Start of the menu code------*/

	var tabMenuLink = $('#tabMenu a');

	tabMenuLink.hover(function() {
		var aTab = $(this);
		aTab.next("em").filter(':not(:animated)').animate({opacity: "show", top: "-140"}, "slow");
		aTab.addClass('mouseover');
	    aTab.removeClass('mouseout');
	}, function() {
		var aTab = $(this);
		aTab.next("em").animate({opacity: "hide", top: "-110"}, "fast");
		aTab.addClass('mouseout');
	    aTab.removeClass('mouseover');
	});


  //Get all the a from the #tabMenu LI
 	tabMenuLink.on('click', function(){
 		var $this = $(this);
    
    //perform the actions when it's not selected
    if (!$this.hasClass('selected')) {    
           
	    //remove the selected class from all LI's a    
	    tabMenuLink.removeClass('selected');
	    
	    //Reassign the a
	    $this.addClass('selected');
	    
	 }
    });

  	/*-------End of the menu code------*/

  	/*-------Start of the registration form code------*/

  	  	$("a#hiddenRegistration").fancybox({
			'scrolling'		: 'no',
			'titleShow'		: false,
			'afterClose'	: function() {
				$('#resetButton').trigger('click');
				$('#register_form span').each(function() {
					$(this).text('');
				});
				$('#register_form img').each(function() {
					$(this).attr('src', 'registration_validation/images/stoperr.png');
				});
				$('#register_form input').each(function() {
					$(this).css('background-color', '');
				});

			}
		});

  	$('#register_btn').on('click', function() {
  		$('a#hiddenRegistration').trigger('click');
  	});

  	$('#signUpBtn').on('click', function(){
  		console.log($('#register_form').serialize());
  	});

  	/*-------End of the registration form code------*/

  });
